/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
 var threeSumClosest = function(nums, target) { // 双指针优化时间复杂度
    nums.sort(function(a,b){return a-b;})  
    let res={
        value:null,
        cha:null
    };
    console.log(nums)
    for(let i=0;i<nums.length-2;i++){          // 可添加剪枝优化，相同的跳过
        let first=i+1,second=nums.length-1;
        while(first<second){
            let sum=nums[i]+nums[first]+nums[second];
            let cha=Math.abs(sum-target);
            console.log(sum,cha,res,nums[i],nums[first],nums[second]);
            if(res.value===null || res.cha>cha){
                res.value=sum;
                res.cha=cha;
            }
            if(sum>target){
                second--;
            }
            else{
                first++;
            }
        }
    }
    console.log(res);
    return res.value;
};
let nums=[0,2,1,-3];
console.log(threeSumClosest(nums,1))